<template>
  <div style="padding: 10px">
    <div>
      <el-button type="primary" @click="add">新增</el-button>
      <el-button type="primary">导入</el-button>
      <el-button type="primary">导出</el-button>
    </div>
    <div style="margin: 10px 0">
      <el-input v-model="search" placeholder="请输入关键字" style="width: 20%" clearable></el-input>
      <el-button type="primary" style="margin: 10px" @click="load">查询</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column fixed prop="id" sortable label="ID" width="150"/>
      <el-table-column prop="title" label="标题" width="120"/>
      <el-table-column prop="author" label="作者" width="120"/>
      <el-table-column prop="time" label="日期" width="120"/>
      <el-table-column fixed="right" label="操作" width="120">
        <template #default="scope">
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm title="确认删除?" @confirm="handleDelete(scope.row)">
            <template #reference>
              <el-button type="text">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin:10px 0">
      <el-pagination
          :page-size="20"
          :pager-count="11"
          layout="prev, pager, next"
          :total="100"
      >
      </el-pagination>

      <el-dialog v-model="dialogVisible" title="Tips" width="50%">
        <el-form :model="form" label-width="120px">
          <el-form-item label="标题">
            <el-input :model="form.title" style="width: 50%"></el-input>
          </el-form-item>
<!--          <div id="div1"></div>-->
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </span>
        </template>
      </el-dialog>
    </div>
  </div>

</template>

<script>
import E from "wangeditor"
import request from "@/utils/request";
let editor;
export default {
  name: 'News',
  components: {},
  data() {
    return {
      form: {},
      dialogVisible: false,
      search: '',
      currentPage: 1,
      total: 10,
      tableData: []
    }
  },
  created() {
    this.load()
  },
  methods: {
    filesUploadSuccess(res){
      console.log(res)
      this.form.cover=res.data
    },
    load() {
      request.get("/news", {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.search
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.data.records
      })
    },
    add() {
      this.dialogVisible = true
      this.form = {}
      this.$nextTick(()=>{
        // editor = new E("#div1")
        // editor.create()
      })

    },
    save() {
      console.log(11)
      // console.log(editor.txt.html())
      if (this.form.id) {
        //更新
        request.put("/news", this.form).then(res => {
          console.log(res);
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "更新成功"
            })
          }
          this.load();
          this.dialogVisible=false;
        })
      } else {
        request.post("/news", this.form).then(res => {
          console.log(res);
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "新增成功"
            })
          }
          this.load();
          this.dialogVisible=false;
        })
      }
    },
    handleDelete(row){
      console.log(row.id);
      request.delete("/news/"+row.id).then(res=>{
        if(res.code === '0'){
          this.$message({
            type:"success",
            message:"删除成功"
          })
        }else {
          this.$message({
            type:"error",
            message:"删除失败"
          })
        }
      });
      this.load();
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true

    }
  }

}

</script>